package com.wc.算法基础课.A第一讲基础算法.归并排序.小朋友排队;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/3/8 0:42
 * @description https://www.acwing.com/problem/content/description/1217/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 100010;
    static Pair[] a = new Pair[N];
    static Pair[] p = new Pair[N];
    // 记录每个人的交换次数,他是不是要和前面比他大的交换,以及后面比他小的交换
    static int[] num = new int[N];
    static int n;

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            a[i] = new Pair(sc.nextInt(), i);
        }
        merge(1, n);
        long res = 0;
        for(int i = 1; i <= n; i++) out.print(num[i] + " ");
        out.println();
        for (int i = 1; i <= n; i++) res += (long) (num[i] + 1) * num[i] / 2;
        out.println(res);
        out.flush();
    }

    static void merge(int l, int r) {
        if (l >= r) return;
        int mid = l + r >> 1;
        merge(l, mid);
        merge(mid + 1, r);
        int i = l, j = mid + 1, k = 0;
        while (i <= mid && j <= r) {
            if (a[i].x <= a[j].x) {
                // 计算后面有多少个比自己小的
                num[a[i].y] += j - mid - 1;
                p[k++] = a[i++];
            } else {
                // 计算后面有前面多少个比自己大的
                num[a[j].y] += mid - i + 1;
                p[k++] = a[j++];
            }
        }
        while (i <= mid) {
            // 这说明了后面全比自己小
            num[a[i].y] += j - mid - 1;
            p[k++] = a[i++];
        }
        while (j <= r) p[k++] = a[j++];
        for (i = l, j = 0; i <= r; i++, j++) a[i] = p[j];
    }

    static class Pair implements Comparable<Pair> {
        int x, y;

        Pair(int x, int y) {
            this.x = x;
            this.y = y;
        }

        @Override
        public int compareTo(Pair o) {
            if (x == o.x) {
                return y - o.y;
            }
            return x - o.x;
        }
    }

}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
